package com.bingxue.edu.tools;

import com.bingxue.edu.common.enums.ChangeTypeEnum;
import com.bingxue.edu.common.enums.OperateStatusEnum;
import com.bingxue.edu.management.education.model.entity.EnrollChangeRecord;
import com.bingxue.edu.management.education.model.entity.LessonStudent;
import com.bingxue.edu.management.education.service.EnrollChangeRecordService;
import com.bingxue.edu.management.education.service.LessonStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static com.bingxue.edu.management.education.model.entity.table.EnrollChangeRecordTableDef.ENROLL_CHANGE_RECORD;

/**
 * @author panbo
 * @since 2025/2/14
 */
@SpringBootTest
public class LassLessonClassIdFixedTool {

    @Autowired
    private EnrollChangeRecordService enrollChangeRecordService;
    @Autowired
    private LessonStudentService lessonStudentService;

//    @Test
//    @Transactional(rollbackFor = Exception.class)
    public void fixed() {
        List<EnrollChangeRecord> recordList = enrollChangeRecordService.queryChain()
                .where(ENROLL_CHANGE_RECORD.CHANGE_TYPE.eq(ChangeTypeEnum.CANCEL_CLASS))
                .and(ENROLL_CHANGE_RECORD.OPERATE_STATUS.eq(OperateStatusEnum.SUCCESS))
                .and(ENROLL_CHANGE_RECORD.LAST_LESSON_CLASS_ID.isNull())
                .list();
        for (EnrollChangeRecord record : recordList) {
            // 查询最后一次课次
            LessonStudent lessonStudent = lessonStudentService.getLastLessonByEnrollmentId(record.getEnrollmentId());
            if (lessonStudent != null) {
                record.setLastLessonClassId(lessonStudent.getStudentClassId());
                enrollChangeRecordService.updateById(record);
            }
        }
    }
}
